package api;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import api.actions.Open;
import api.actions.Properties;
import api.actions.Save;
import api.actions.SaveAs;
import api.components.ImagePanel;
import api.image.MyImage;

import javax.swing.JToolBar;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.MatteBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import api.image.MyImage;

import java.awt.Font;


public class Interface {
	public static JFileChooser FileChooser;
	public static JMenu menuEdit;
	public static JFrame frame = new JFrame();
	public static JMenuItem mFClose;
	public static JMenuItem mFSave;
	public static JMenuItem mFSaveAs;
	public static JMenuItem mFProperties;
	public static JButton btnCancel;
	public JButton btnOpen;
	public JButton btnProperties;
	public JMenu menuFile;
	public static JTabbedPane panelImagen;
	
	public Interface() {
		frame.setBackground(Color.DARK_GRAY);
		FileChooser = new JFileChooser("./files/images");
		frame.setTitle("VO PhotoEditor");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		int screenHeight = 700;
		int screenWidth = 1280;
		
		frame.setSize(screenWidth, screenHeight);
		frame.setLocation(screenWidth, screenHeight);
				
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		menuFile = new JMenu("Archivo");
		menuBar.add(menuFile);
		
		JMenuItem mFOpen = new JMenuItem("Abrir Archivo...                                   ");
		mFOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		menuFile.add(mFOpen);
		
		menuFile.add(new JSeparator());
		
		mFClose = new JMenuItem("Cerrar", new ImageIcon("./files/icons/close.gif"));
		mFClose.setEnabled(false);
		mFClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		menuFile.add(mFClose);
		
		menuFile.add(new JSeparator());
		
		mFSave = new JMenuItem("Guardar", new ImageIcon("./files/icons/save.gif"));
		mFSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Save.run();
			}
		});
		mFSave.setEnabled(false);
		mFSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		menuFile.add(mFSave);
		
		mFSaveAs = new JMenuItem("Guardar Como...");
		mFSaveAs.setEnabled(false);
		mFSaveAs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SaveAs.run();
			}
		});
		menuFile.add(mFSaveAs);
		
		menuFile.add(new JSeparator());
		
		mFProperties = new JMenuItem("Propiedades");
		mFProperties.setEnabled(false);
		menuFile.add(mFProperties);
		
		menuEdit = new JMenu("Editar");
		menuBar.add(menuEdit);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 102, 51));
		panel.setBorder(new MatteBorder(0, 1, 4, 1, (Color) Color.LIGHT_GRAY));
		
		panelImagen = new JTabbedPane(JTabbedPane.TOP);
		panelImagen.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if (panelImagen.getTabCount() == 1) {
					// funcion que deshabilite todo esto
					
					mFClose.setEnabled(true);
					btnCancel.setEnabled(true);
					mFSave.setEnabled(true);
					mFSaveAs.setEnabled(true);
					mFProperties.setEnabled(true);
					btnProperties.setEnabled(true);
					//Menu.activaOpcionesMenu();
					//InfoLabels.activaEtiquetas();
				} else if (panelImagen.getTabCount() == 0) {
					mFClose.setEnabled(false);
					btnCancel.setEnabled(false);
					mFProperties.setEnabled(false);
					btnProperties.setEnabled(false);
					
					//Menu.desactivaOpcionesMenu();
					//InfoLabels.desactivaEtiquetas();
					//hist.setHistogram(histCero);
					//histAc.setHistogram(histCero2);
					//panelHistograma.repaint();
				}
			}
		});
		
		JPanel panelFooter = new JPanel();
		
		JPanel panel_2 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(42)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addGap(6)
							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 328, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 408, Short.MAX_VALUE)
							.addComponent(panelFooter, GroupLayout.PREFERRED_SIZE, 443, GroupLayout.PREFERRED_SIZE)
							.addGap(53))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panelImagen, GroupLayout.PREFERRED_SIZE, 1196, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(42, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addComponent(panelImagen, GroupLayout.PREFERRED_SIZE, 494, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelFooter, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
					.addGap(108))
		);
		
		JLabel lblPos = new JLabel("x:   y:");
		lblPos.setFont(new Font("Lucida Grande", Font.BOLD, 12));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(22)
					.addComponent(lblPos, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(118, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblPos)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JLabel lblCreditos = new JLabel("Miguel Monterrey Varela   VOPhotoEditor v 0.1");
		lblCreditos.setFont(new Font("Lucida Grande", Font.BOLD, 11));
		lblCreditos.setForeground(Color.LIGHT_GRAY);
		GroupLayout gl_panelFooter = new GroupLayout(panelFooter);
		gl_panelFooter.setHorizontalGroup(
			gl_panelFooter.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panelFooter.createSequentialGroup()
					.addContainerGap(179, Short.MAX_VALUE)
					.addComponent(lblCreditos))
		);
		gl_panelFooter.setVerticalGroup(
			gl_panelFooter.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelFooter.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCreditos)
					.addContainerGap(8, Short.MAX_VALUE))
		);
		panelFooter.setLayout(gl_panelFooter);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBackground(new Color(255, 102, 51));
		toolBar.setFloatable(false);
		
		JLabel lblVophotoeditor = new JLabel("VOPhotoEditor");
		lblVophotoeditor.setFont(new Font("Lucida Grande", Font.BOLD, 22));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(40)
					.addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
					.addGap(388)
					.addComponent(lblVophotoeditor)
					.addContainerGap(542, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(20)
							.addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(28)
							.addComponent(lblVophotoeditor)))
					.addContainerGap(25, Short.MAX_VALUE))
		);
		
		JButton btnOpen = new JButton("");
		btnOpen.setIcon(new ImageIcon("./files/icons/page.png"));
		toolBar.add(btnOpen);
		
		btnCancel = new JButton("");
		btnCancel.setEnabled(false);
		btnCancel.setIcon(new ImageIcon("./files/icons/pageclose_32.png"));
		toolBar.add(btnCancel);
		
		btnProperties = new JButton("");
		btnProperties.setEnabled(false);
		btnProperties.setIcon(new ImageIcon("./files/icons/window_app_list.png"));
		toolBar.add(btnProperties);
		
		ActionListener propertiesAL = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Properties.run();
			}
		};
		
		btnProperties.addActionListener(propertiesAL);
		mFProperties.addActionListener(propertiesAL);
		
		panel.setLayout(gl_panel);
		frame.getContentPane().setLayout(groupLayout);		
		
		ActionListener cerrarAL = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	            RemoveSelectedImage();
			}
		};
		
		ActionListener abrirAL = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Open.run();
			}
		};
		
		mFClose.addActionListener(cerrarAL);
		btnCancel.addActionListener(cerrarAL);
		
		mFOpen.addActionListener(abrirAL);
		btnOpen.addActionListener(abrirAL);
		
		FileChooser.setAcceptAllFileFilterUsed(false);
		FileChooser.setFileFilter(new FileNameExtensionFilter("archivos TIFF", "tif"));
		FileChooser.setFileFilter(new FileNameExtensionFilter("archivos BMP", "bmp"));
	}
	
	public void visualizar() {
		frame.setVisible(true);
	}
	
	public static MyImage getImage(int index) {
		return ((ImagePanel) ((JScrollPane) panelImagen.getComponentAt(index)).getViewport().getView()).image;
	}
	
	public static MyImage getSelectedImage() {
		return getImage(getSelectedImageIndex());
	}
	
	public static void RemoveSelectedImage() {
		panelImagen.remove(panelImagen.getSelectedIndex());
	}
	
	public static int getSelectedImageIndex(){
		return panelImagen.getSelectedIndex();
	}	
}
